from sqlalchemy import (
    Column, BigInteger, String, Text, DateTime, Boolean, Integer, 
    Enum, ForeignKey, JSON, text, DECIMAL
)
from sqlalchemy.orm import relationship
from ..database import Base
import enum


class NovelStatusEnum(enum.Enum):
    ongoing = "ongoing"
    completed = "completed"
    paused = "paused"


class RoleTypeEnum(enum.Enum):
    protagonist = "protagonist"
    supporting = "supporting"
    antagonist = "antagonist"
    minor = "minor"


class Novel(Base):
    __tablename__ = "novels"

    id = Column(BigInteger, primary_key=True, index=True, comment='小说ID')
    title = Column(String(200), nullable=False, comment='小说标题')
    author = Column(String(100), nullable=False, comment='作者')
    description = Column(Text, nullable=True, comment='小说简介')
    cover_image = Column(String(255), nullable=True, comment='封面图片URL')
    genre = Column(String(50), nullable=True, comment='类型')
    tags = Column(JSON, nullable=True, comment='标签')
    status: Column[NovelStatusEnum] = Column(
        Enum(NovelStatusEnum), 
        default=NovelStatusEnum.ongoing, 
        comment='状态'
    )
    chapters = Column(Integer, default=0, comment='章节数')
    word_count = Column(BigInteger, default=0, comment='字数')
    rating: Column[float] = Column(DECIMAL(3, 1), default=0.0, comment='评分')
    view_count = Column(BigInteger, default=0, comment='阅读数')
    discussion_count = Column(Integer, default=0, comment='讨论数')
    created_at = Column(
        DateTime(timezone=False), 
        server_default=text('CURRENT_TIMESTAMP'),
        comment='创建时间'
    )
    updated_at = Column(
        DateTime(timezone=False), 
        server_default=text('CURRENT_TIMESTAMP'),
        onupdate=text('CURRENT_TIMESTAMP'),
        comment='更新时间'
    )
    is_active = Column(Boolean, default=True, comment='是否激活')

    # 关系
    characters = relationship(
        "NovelCharacter", 
        back_populates="novel", 
        cascade="all, delete-orphan"
    )


class NovelCharacter(Base):
    __tablename__ = "novel_characters"

    id = Column(BigInteger, primary_key=True, index=True, comment='角色ID')
    novel_id = Column(
        BigInteger, 
        ForeignKey('novels.id', ondelete='CASCADE'), 
        nullable=False, 
        comment='所属小说ID'
    )
    name = Column(String(100), nullable=False, comment='角色名称')
    description = Column(Text, nullable=True, comment='角色描述')
    personality = Column(Text, nullable=True, comment='性格特征')
    background = Column(Text, nullable=True, comment='背景故事')
    abilities = Column(JSON, nullable=True, comment='能力技能')
    relationships = Column(JSON, nullable=True, comment='人物关系')
    avatar_image = Column(String(255), nullable=True, comment='角色头像')
    role_type: Column[RoleTypeEnum] = Column(
        Enum(RoleTypeEnum), 
        default=RoleTypeEnum.supporting, 
        comment='角色类型'
    )
    importance_level = Column(Integer, default=5, comment='重要程度(1-10)')
    tags = Column(JSON, nullable=True, comment='标签')
    created_at = Column(
        DateTime(timezone=False), 
        server_default=text('CURRENT_TIMESTAMP'),
        comment='创建时间'
    )
    updated_at = Column(
        DateTime(timezone=False), 
        server_default=text('CURRENT_TIMESTAMP'),
        onupdate=text('CURRENT_TIMESTAMP'),
        comment='更新时间'
    )
    is_active = Column(Boolean, default=True, comment='是否激活')

    # 关系
    novel = relationship("Novel", back_populates="characters") 